<!doctype html>
<html lang="en-us">
<head>

    <meta charset="utf-8">
    <meta name="generator" content="Hugo 0.59.1" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Mybatis分页插件PageHelper | 林贤钦</title>
    <meta property="og:title" content="Mybatis分页插件PageHelper - 林贤钦">
    <meta property="og:type" content="article">
        
    <meta property="article:published_time" content="2019-12-13T12:37:39&#43;08:00">
        
        
    <meta property="article:modified_time" content="2019-12-13T12:37:39&#43;08:00">
        
    <meta name="Keywords" content="java,博客,项目管理,软件架构,公众号,小程序">
    <meta name="description" content="Mybatis分页插件PageHelper">
        
    <meta name="author" content="林贤钦">
    <meta property="og:url" content="http://linxianqin.gitee.io/post/Mybatis%E5%88%86%E9%A1%B5%E6%8F%92%E4%BB%B6PageHelper/">
    <link rel="shortcut icon" href="/img/favicon.ico" type="image/x-icon">

    <link rel="stylesheet" href="/css/normalize.css">
    
        <link rel="stylesheet" href="/css/prism.css">
    
    <link rel="stylesheet" href="/css/style.css">
    <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

    


    
    
</head>

<body>
<header id="header" class="clearfix">
	<div class="container">
        <div class="col-group">
            <div class="site-name ">
                
                    <a id="logo" href="http://linxianqin.gitee.io">
                        林贤钦
                    </a>
                
                <p class="description">专注于Java、微信小程序、移动互联网、项目管理、软件架构</p>
            </div>
            <div>
                <nav id="nav-menu" class="clearfix">
                    <a class="current" href="http://linxianqin.gitee.io">首页</a>
                    
                    <a  href="http://linxianqin.gitee.io/archives/" title="归档">归档</a>
                    
                    <a  href="http://linxianqin.gitee.io/about/" title="关于">关于</a>
                    
                </nav>
            </div>
        </div>
    </div>
</header>


<div id="body">
        
        
    <div class="container">
        <div class="col-group">

            <div class="col-8" id="main">
                <div class="res-cons">
                    <article class="post">
                        <header>
                            <h1 class="post-title">Mybatis分页插件PageHelper</h1>
                        </header>
                        <date class="post-meta meta-date">
                            2019年12月13日
                        </date>
                        
                        <div class="post-meta">
                            <span>|</span>
                            
                                <span class="meta-category"><a href="http://linxianqin.gitee.io/categories/java">java</a></span>
                            
                        </div>
                        
                        
                        <div class="post-meta">
                            <span id="busuanzi_container_page_pv">|<span id="busuanzi_value_page_pv"></span><span> 阅读</span></span>
                        </div>
                        
                        
                        <div class="post-content">
							<nav id="TableOfContents">
<ul>
<li>
<ul>
<li>
<ul>
<li><a href="#pagehelper的配置">PageHelper的配置</a></li>
<li><a href="#pagehelper的使用">PageHelper的使用</a></li>
<li><a href="#特别注意">特别注意</a></li>
</ul></li>
</ul></li>
</ul>
</nav>
                            

<h3 id="pagehelper的配置">PageHelper的配置</h3>

<ol>
<li><p>添加PageHelper依赖</p>

<pre><code class="language-xml">	&lt;!-- mybatis 分页插件 --&gt;
&lt;!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper --&gt;
&lt;dependency&gt;
  &lt;groupId&gt;com.github.pagehelper&lt;/groupId&gt;
  &lt;artifactId&gt;pagehelper&lt;/artifactId&gt;
  &lt;version&gt;5.1.10&lt;/version&gt;
&lt;/dependency&gt;
</code></pre></li>

<li><p>在mybatis-config.xml中添加PageHelper配置</p>

<pre><code class="language-xml">&lt;configuration&gt;
&lt;settings&gt;
    &lt;setting name=&quot;mapUnderscoreToCamelCase&quot; value=&quot;true&quot;/&gt;
    &lt;setting name=&quot;cacheEnabled&quot; value=&quot;true&quot; /&gt;
    &lt;!-- 开启mybatis 延迟加载--&gt;
    &lt;setting name=&quot;lazyLoadingEnabled&quot; value=&quot;false&quot;/&gt;
    &lt;!--        &lt;setting name=&quot;logImpl&quot; value=&quot;LOG4J2&quot; /&gt;--&gt;
&lt;/settings&gt;
&lt;!-- 给包起别名可以在这里起 --&gt;
&lt;typeAliases&gt;
    &lt;!-- 用于指定要配置别名的包，当指定之后，该包下的实体类都会注册别名，并且类名就是别名，不再区分大小写--&gt;
    &lt;package name=&quot;com.medicalSaleManagementSystem.core.model.entity&quot;/&gt;
&lt;/typeAliases&gt;

&lt;!-- 这里是mybatis的分页插件 --&gt;
&lt;plugins&gt;
    &lt;!-- com.github.pagehelper为PageHelper类所在包名 --&gt;
    &lt;plugin interceptor=&quot;com.github.pagehelper.PageInterceptor&quot;&gt;
        &lt;property name=&quot;reasonable&quot; value=&quot;true&quot;/&gt;
        &lt;!-- 使用下面的方式配置参数 --&gt;
        &lt;!--            &lt;property name=&quot;param1&quot; value=&quot;value1&quot;/&gt;--&gt;
    &lt;/plugin&gt;
&lt;/plugins&gt;
&lt;/configuration&gt;
</code></pre></li>
</ol>

<h3 id="pagehelper的使用">PageHelper的使用</h3>

<ol>
<li><p>在service层UserServiceImpl</p>

<pre><code class="language-java">@Service
@Transactional
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
	
	@Override
public List&lt;User&gt; getAll() {
    return userMapper.selectByExample(null);
}
}
</code></pre></li>

<li><p>在controller层</p>

<pre><code class="language-java">@Controller
@Scope(value = &quot;prototype&quot;)
public class UserController {
@Autowired
private UserService userService;

@RequestMapping(&quot;/user&quot;)
public String index() {
    return &quot;/userManagement/user&quot;;
}

	/*
 * 功能描述: &lt;br&gt;
 * 〈〉获取用户所有数据
 * @Param:
 * @Return:
 * @Author: 林贤钦
 * @Date: 2019/12/13 1:20
 */
@RequestMapping (value = &quot;/user&quot;,method = RequestMethod.GET)
@ResponseBody
public Resp getAll(@RequestParam(value = &quot;pn&quot;, defaultValue = &quot;1&quot;) int pn){
    try{
        //设置现在的页数为1，显示的条数为5条
        PageHelper.startPage(pn, 5);
        List&lt;User&gt; userList = userService.getAll();
        //取分页信息，PageInfo。
        PageInfo&lt;User&gt; pageInfo  = new PageInfo&lt;&gt;(userList);
        return Resp.httpStatus(HttpStatus.OK,&quot;查找用户信息成功！&quot;,pageInfo);
    }catch (Exception e){
        e.printStackTrace();
    }
    //500
    return Resp.httpStatus(HttpStatus.INTERNAL_SERVER_ERROR,&quot;系统内部错误&quot;);
}
}
</code></pre></li>
</ol>

<h3 id="特别注意">特别注意</h3>

<p>这三条，一定要按顺序，本人表示入坑。</p>

<pre><code class="language-java"> PageHelper.startPage(pn, 5);
 List&lt;User&gt; userList = userService.getAll();
 PageInfo&lt;User&gt; pageInfo  = new PageInfo&lt;&gt;(userList);
</code></pre>

                        </div>
						 
                        
<div class="post-archive">
    <ul class="post-copyright">
        <li><strong>原文作者：</strong><a rel="author" href="http://linxianqin.gitee.io">林贤钦</a></li>
        <li style="word-break:break-all"><strong>原文链接：</strong><a href="http://linxianqin.gitee.io/post/Mybatis%E5%88%86%E9%A1%B5%E6%8F%92%E4%BB%B6PageHelper/">http://linxianqin.gitee.io/post/Mybatis%E5%88%86%E9%A1%B5%E6%8F%92%E4%BB%B6PageHelper/</a></li>
        <li><strong>版权声明：</strong>本作品采用<a rel="license" href="https://linxianqin.gitee.io">知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议</a>进行许可，非商业转载请注明出处（作者，原文链接），商业转载请联系作者获得授权。</li>
    </ul>
</div>
<br/>



                        

<div class="post-archive">
    <h2>See Also</h2>
    <ul class="listing">
        
        <li><a href="/post/Restful%E7%BB%9F%E4%B8%80Json%E5%B7%A5%E5%85%B7%E7%B1%BB%E5%B8%A6%E5%88%86%E9%A1%B5%E4%BF%A1%E6%81%AF/">Restful统一Json工具类，带分页信息</a></li>
        
        <li><a href="/post/ssm&#43;vue%E8%B7%A8%E5%9F%9F%E8%A7%A3%E5%86%B3%E5%AE%9E%E4%BE%8B/">ssm&#43;vue跨域解决实例</a></li>
        
        <li><a href="/post/intBeanUtilEx%E5%B7%A5%E5%85%B7%E7%B1%BB/">BeanUtilEx工具类解决Integer=0和sql.date异常的问题</a></li>
        
        <li><a href="/post/mybatis%E9%80%86%E5%90%91%E5%B7%A5%E7%A8%8B-%E8%87%AA%E5%8A%A8%E7%94%9F%E6%88%90%E4%BB%A3%E7%A0%81/">mybatis逆向工程---自动生成代码</a></li>
        
        <li><a href="/post/springMVC%E8%AF%B7%E6%B1%82%E5%8F%82%E6%95%B0%E7%9A%84%E7%BB%91%E5%AE%9A/">springMVC请求参数的绑定</a></li>
        
    </ul>
</div>


                        <div class="post-meta meta-tags">
                            
                            <ul class="clearfix">
                                
                                <li><a href="http://linxianqin.gitee.io/tags/ssm">ssm</a></li>
                                
                            </ul>
                            
                        </div>
                    </article>
                    
    

    
    
    <div class="post bg-white">
      <script src="https://utteranc.es/client.js"
            repo= "757610938/hugo-blogs-utterances"
            issue-term="pathname"
            theme="github-light"
            crossorigin="anonymous"
            async>
      </script>
    </div>
    
                </div>
            </div>
            <div id="secondary">
    <section class="widget">
        <form id="search" action="//www.google.com/search" method="get" accept-charset="utf-8" target="_blank" _lpchecked="1">
      
      <input type="text" name="q" maxlength="20" placeholder="Search">
      <input type="hidden" name="sitesearch" value="http://linxianqin.gitee.io">
      <button type="submit" class="submit icon-search"></button>
</form>
    </section>
    
    <section class="widget">
        <h3 class="widget-title">最近文章</h3>
<ul class="widget-list">
    
    <li>
        <a href="http://linxianqin.gitee.io/post/java-%E5%8F%8D%E5%B0%84/" title="Java 反射">Java 反射</a>
    </li>
    
    <li>
        <a href="http://linxianqin.gitee.io/post/java-%E9%93%BE%E8%A1%A8/" title="Java 链表">Java 链表</a>
    </li>
    
    <li>
        <a href="http://linxianqin.gitee.io/post/%E7%9F%AD%E4%BF%A1%E6%B3%A8%E5%86%8C%E5%92%8C%E5%BE%AE%E4%BF%A1%E7%99%BB%E5%BD%95%E7%9A%84%E5%AE%9E%E7%8E%B0/" title="短信注册和微信登录的实现">短信注册和微信登录的实现</a>
    </li>
    
    <li>
        <a href="http://linxianqin.gitee.io/post/OAuth2.0%E8%AE%A4%E8%AF%81%E6%8E%88%E6%9D%83%E5%8D%95%E7%82%B9%E7%99%BB%E5%BD%95/" title="OAuth2.0认证授权单点登录">OAuth2.0认证授权单点登录</a>
    </li>
    
    <li>
        <a href="http://linxianqin.gitee.io/post/Nacos%E6%9C%8D%E5%8A%A1%E6%B3%A8%E5%86%8C%E4%B8%AD%E5%BF%83/" title="Nacos服务注册中心">Nacos服务注册中心</a>
    </li>
    
    <li>
        <a href="http://linxianqin.gitee.io/post/SpringCloud%E5%BE%AE%E6%9C%8D%E5%8A%A1/" title="SpringCloud微服务">SpringCloud微服务</a>
    </li>
    
    <li>
        <a href="http://linxianqin.gitee.io/post/%E9%98%BF%E9%87%8C%E4%BA%91%E8%A7%86%E9%A2%91%E7%82%B9%E6%92%AD%E6%9C%8D%E5%8A%A1/" title="阿里云视频点播服务前后端实现">阿里云视频点播服务前后端实现</a>
    </li>
    
    <li>
        <a href="http://linxianqin.gitee.io/post/%E9%98%9F%E5%88%97/" title="队列">队列</a>
    </li>
    
    <li>
        <a href="http://linxianqin.gitee.io/post/%E7%A8%80%E7%96%8F%E6%95%B0%E7%BB%84/" title="稀疏数组">稀疏数组</a>
    </li>
    
    <li>
        <a href="http://linxianqin.gitee.io/post/%E9%98%BF%E9%87%8C%E4%BA%91EasyExcel%E5%AE%9E%E7%8E%B0%E5%90%8E%E7%AB%AF%E5%AF%B9Excel%E7%9A%84%E8%AF%BB%E5%86%99%E6%93%8D%E4%BD%9C/" title="使用阿里云EasyExcel实现后端对Excel的读写操作">使用阿里云EasyExcel实现后端对Excel的读写操作</a>
    </li>
    
</ul>
    </section>

    

    <section class="widget">
        <h3 class="widget-title">分类</h3>
<ul class="widget-list">
    
    <li><a href="http://linxianqin.gitee.io/categories/JWT/">JWT (2)</a></li>
    
    <li><a href="http://linxianqin.gitee.io/categories/hugo/">hugo (2)</a></li>
    
    <li><a href="http://linxianqin.gitee.io/categories/java/">java (18)</a></li>
    
    <li><a href="http://linxianqin.gitee.io/categories/java8/">java8 (1)</a></li>
    
    <li><a href="http://linxianqin.gitee.io/categories/springCloud/">springCloud (2)</a></li>
    
    <li><a href="http://linxianqin.gitee.io/categories/springSecurity/">springSecurity (4)</a></li>
    
    <li><a href="http://linxianqin.gitee.io/categories/vue/">vue (5)</a></li>
    
    <li><a href="http://linxianqin.gitee.io/categories/yml/">yml (1)</a></li>
    
    <li><a href="http://linxianqin.gitee.io/categories/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/">数据结构 (3)</a></li>
    
    <li><a href="http://linxianqin.gitee.io/categories/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/">软件测试 (1)</a></li>
    
    <li><a href="http://linxianqin.gitee.io/categories/%E9%98%BF%E9%87%8C%E4%BA%91/">阿里云 (4)</a></li>
    
</ul>
    </section>

    <section class="widget">
        <h3 class="widget-title">标签</h3>
<div class="tagcloud">
    
    <a href="http://linxianqin.gitee.io/tags/JWT/">JWT</a>
    
    <a href="http://linxianqin.gitee.io/tags/hugo/">hugo</a>
    
    <a href="http://linxianqin.gitee.io/tags/java/">java</a>
    
    <a href="http://linxianqin.gitee.io/tags/java8/">java8</a>
    
    <a href="http://linxianqin.gitee.io/tags/spring/">spring</a>
    
    <a href="http://linxianqin.gitee.io/tags/springBoot/">springBoot</a>
    
    <a href="http://linxianqin.gitee.io/tags/springCloud/">springCloud</a>
    
    <a href="http://linxianqin.gitee.io/tags/ssm/">ssm</a>
    
    <a href="http://linxianqin.gitee.io/tags/vue/">vue</a>
    
    <a href="http://linxianqin.gitee.io/tags/yml/">yml</a>
    
    <a href="http://linxianqin.gitee.io/tags/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/">数据结构</a>
    
    <a href="http://linxianqin.gitee.io/tags/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/">软件测试</a>
    
    <a href="http://linxianqin.gitee.io/tags/%E9%98%BF%E9%87%8C%E4%BA%91/">阿里云</a>
    
</div>
    </section>

    
<section class="widget">
    <h3 class="widget-title">友情链接</h3>
    <ul class="widget-list">
        
        <li>
            <a target="_blank" href="https://www.cnki.net" title="中国知网">中国知网</a>
        </li>
        
        <li>
            <a target="_blank" href="https://linxianqin.com" title="个人的另一个博客网站(国内加载慢)">个人的另一个博客网站(国内加载慢)</a>
        </li>
        
    </ul>
</section>


    <section class="widget">
        <h3 class="widget-title">其它</h3>
        <ul class="widget-list">
            <li><a href="http://linxianqin.gitee.io/index.xml">文章 RSS</a></li>
        </ul>
    </section>
</div>
        </div>
    </div>
</div>
<footer id="footer">
    <div class="container">
        &copy; 2020 <a href="http://linxianqin.gitee.io">林贤钦 By 林贤钦</a>.
        Powered by <a rel="nofollow noreferer noopener" href="https://gohugo.io" target="_blank">Hugo</a>.
        <a href="https://www.flysnow.org/" target="_blank">Theme</a> based on <a href="https://github.com/rujews/maupassant-hugo" target="_blank">maupassant</a>.
        
    </div>
</footer>


    
    <script type="text/javascript">
        
        (function () {
            $("pre code").parent().addClass("line-numbers")
        }());

        window.MathJax = {
            tex2jax: {
                inlineMath: [['$', '$']],
                processEscapes: true
                }
            };
    </script>
    <script type="text/javascript" src="/js/prism.js" async="true"></script>
    <script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML' async></script>


<a id="rocket" href="#top"></a>
<script type="text/javascript" src="/js/totop.js?v=0.0.0" async=""></script>

<script type="application/javascript">
var doNotTrack = false;
if (!doNotTrack) {
	window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
	ga('create', 'GA ID', 'auto');
	
	ga('send', 'pageview');
}
</script>
<script async src='https://www.google-analytics.com/analytics.js'></script>



    <script type="text/javascript" src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" async></script>




</body>
</html>
